package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.Category;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CategoryMapper {

    // 查询全部类别
    @Select("SELECT * FROM category")
    @Results(id = "categoryMap",value = {
            @Result(property = "id", column = "id"),
            @Result(property = "products", column = "id",
                    many = @Many(select = "edu.gdkm.sql.dao.ProductMapper.findByTypeid"))
    })
    List<Category> findAll();

    // 根据id查询类别
    @Select("SELECT * FROM category WHERE id = #{id}")
    Category findById(int id);

    // 添加类别
    @Insert("INSERT INTO category(typename) VALUES(#{typename})")
    int insertCategory(Category category);

    // 更新类别
    @Update("UPDATE category SET typename=#{typename} WHERE id=#{id}")
    int updateCategoryById(Category category);

    // 删除类别
    @Delete("DELETE FROM category WHERE id=#{id}")
    int deleteCategoryById(int id);
}